<?php
/**
 * Created by PhpStorm.
 * User: david
 * Date: 2018/5/25
 * Time: 14:59
 */

namespace app\controller;

use app\model\Post as PostModel;
use app\model\ProxyProject as ProxyProjectModel;
use app\model\PostMenu as PostMenuModel;
use think\Request;

class Post extends Base
{
    public function index(Request $request)
    {
        $act = 3;
        $i = 'item1';
        $posts = PostModel::getPosts($request->param());
        if ($posts->items()) {
            $t = $posts->items()[0];
        } else {
            $t = null;
        }
        $proxy = ProxyProjectModel::getAll();
        $menu = PostMenuModel::get($request->param('pid'));
        return view('post', compact(['act', 'i', 'posts', 't', 'proxy', 'menu']));
    }

    public function detail($id)
    {
        $act = 3;
        $i = 'item2';
        $post = PostModel::get($id);
        $proxy = ProxyProjectModel::getAll();
        return view('detail', compact(['act', 'i', 'post', 'proxy']));
    }

    public function page($page)
    {
        $posts = PostModel::getPostsByPage($page);
        if ($posts) {
            return [
                'code' => 0,
                'msg' => 'success',
                'data' => $posts
            ];
        } else {
            return [
                'code' => 1,
                'msg' => '没有更多了'
            ];
        }
    }
}